package com.dongdong.android.framework.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.dongdong.android.framework.R;
import com.dongdong.android.framework.dialog.LoadingDialog;
import com.dongdong.android.framework.manager.AppManager;
import com.dongdong.android.framework.mvp.BaseModel;
import com.dongdong.android.framework.mvp.BasePresenter;
import com.dongdong.android.framework.utils.NetWorkUtil;
import com.dongdong.android.framework.utils.TUtil;
import com.dongdong.android.framework.utils.ToastUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

/**
 * @author dongdongzheng
 * @date 2018/3/27
 */

public abstract class BaseActivity<T extends BasePresenter, E extends BaseModel> extends RxAppCompatActivity implements View.OnClickListener {

    protected Context mContext;
    protected T mPresenter;
    protected E mModel;

    private IntentFilter mIntentFilter;
    public LoadingDialog mDialog;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mContext = this;
        mPresenter = TUtil.getT(this, 0);
        mModel = TUtil.getT(this, 1);

        this.setContentView(this.getLayoutId());
        this.initView();
        this.initData();
        this.initListener();
        this.initPresenter();

        AppManager.getInstance().addActivity(this);

        mIntentFilter = new IntentFilter();
        wrapBroadcastReceiver(mIntentFilter);
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver, mIntentFilter);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
        LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
        AppManager.getInstance().finishActivity(this);
    }

    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();


    /**
     * 简单页面无需mvp就不用管此方法即可,完美兼容各种实际场景的变通
     */
    public abstract void initPresenter();


    /**
     * 用于注册广播 添加Action
     */
    protected void wrapBroadcastReceiver(IntentFilter filter) {
    }

    /**
     * 收到广播的回调
     *
     * @param intent 广播的Intent
     */
    protected void dealBroadCastReceiver(Intent intent) {

    }

    private void dispatchBroadCastReceiver(Intent intent) {
        dealBroadCastReceiver(intent);
    }

    final BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            dispatchBroadCastReceiver(intent);
        }
    };


    /**
     * show the progress dialog
     */
    public void showProgressDialog() {
        if (NetWorkUtil.isConnectingToInternet()) {
            if (mDialog == null) {
                mDialog = new LoadingDialog(mContext);
            }
            mDialog.setCanceledOnTouchOutside(false);
            mDialog.setCancelable(true);

            if (mDialog.isShowing() == false) {

                mDialog.show();
            }
        } else {
            ToastUtil.showLongToast(mContext, R.string.common_dialog_no_network);
        }
    }

    /**
     * Dismiss the progress dialog
     */
    public void dismissProgressDialog() {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }


}
